package action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modle.Club;
import modle.Member;
import modle.Membership;
import modle.MembershipId;
import modle.Stadium;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;

import util.SessionFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @描述 获得该用户可见的游戏房间 stadiums
 * @author GaoTong
 * @create 2013/1/16
 */
public class RoomListAction extends ActionSupport{
	
	public List<Stadium> getRooms() {
		return rooms;
	}

	public void setRooms(List<Stadium> rooms) {
		this.rooms = rooms;
	}


	private List<Stadium> rooms = new ArrayList<Stadium>();
	private int totalNum;

	
 	private Session session = SessionFactory.getSession();
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
//		Transaction tran =  session.beginTransaction();
		
		Map<String,Object> httpSession = ActionContext.getContext().getSession();
		String username = (String) httpSession.get("username");
		System.out.println(ActionContext.getContext());
		System.out.println(session);
		System.out.println("检测到用户已经登录:" + httpSession.get("username"));

		//如果用户已 登录
		if(username != null){
			//先查询到该用户
			Query query = session.createQuery("from Member m where m.memberName = ?");
			query.setString(0, username);
			Member member = (Member) query.uniqueResult();
			
			System.out.println();
			Set<Club> set = member.getClubs();
			
			System.out.println("clubs:"+set);
			
			//得到该用户加入的clubs
			for(Club club:set){
				Set<Stadium> stadiums = club.getStadiums();
				
				System.out.println("stadiums:" + stadiums);
				
				for(Stadium s:stadiums){
					int levle = s.getAccessSecurityLevel();
					Membership ms = (Membership) session.get(Membership.class, new MembershipId(member,club));
					
					if(ms != null){
						System.out.println("ClubSecurityLevel:" + ms.getClubSecurityLevel());
						
						//最后的判断 用户的权限， 得到该用户的 游戏房间,  
						if(ms.getClubSecurityLevel() >= levle){
							rooms.add(s);
						}
					}
				}
				
			}
			
			//session.createQuery("from Stadium s where s.accessSecurityLevel <= 100").list();
			
			System.out.println("rooms size:" + rooms.size());
		
		}else{
			
			//按guest来查询房间列表
			rooms = session.createQuery("from Stadium s where s.accessSecurityLevel <= 100").list();
			System.out.println("来宾房间列表：" + rooms.size());
			
			HttpServletRequest request = ServletActionContext.getRequest();
			Cookie[] cookies = request.getCookies();
			String guestName = null;
			if(cookies != null){
				for(Cookie c:cookies){
					if(c.getName().equals("guestName"))
						guestName = c.getValue();
				}
			}
			if(guestName != null){
				httpSession.put("guestName", guestName);
			}
			
			else{
				//如果用户为登录，先拿到他得IP
	    		String ipAddress = (String) httpSession.get("userip");
	    		
	    		Member member = new Member();
	    		
	    		member.setMemberName(ipAddress);
	    		member.setIpAddress(ipAddress);
	    		
	    		member.setSystemSecurityLevel(100);
	    		member.setSuperMembership(1);
	    		
	    		Integer memberId = (Integer) session.save(member);
	    		
	    		//把用户的临时用户名放入session
	    		httpSession.put("guestName", "guest"+memberId);
	    		
				
				
	    		session.flush();
	    		session.close();
	    		
	    		session = SessionFactory.getSession();
	    		
	    		Member newMember = (Member) session.get(Member.class, memberId);
	    		newMember.setMemberName("guest"+memberId);
	    		session.saveOrUpdate(newMember);
	    		session.flush();
	    		session.close();
	    		
	    		
	    		HttpServletResponse response = ServletActionContext.getResponse();
	    		Cookie cookie = new Cookie("guestName", "guest"+memberId);
	    		cookie.setMaxAge(Integer.MAX_VALUE);
	    		response.addCookie(cookie);
	    		
			}
			
			
		}
		
		if(session.isOpen()){
			session.flush();
			session.close();
		}
		
		
		
		return "success";
	}
	


	 
}
